//
//  MineMefocusViewController.m
//  Tongcheng
//
//  Created by 邢广范 on 2017/4/9.
//  Copyright © 2017年 Tongcheng. All rights reserved.
//

#import "MineMefocusViewController.h"
#import "MineFocusOperator.h"
#import "MineFocusModel.h"
#import "MineFocusDataModel.h"
#import "MineFocusListModel.h"
#import "MineMeFocusTableViewCell.h"
#import "MJRefresh.h"
#import "CancelFocusOperator.h"

@interface MineMefocusViewController ()<UITableViewDelegate, UITableViewDataSource,MineMeFocusTableViewCellDelegate>

@property (nonatomic, assign) NSInteger page;

@property (weak, nonatomic) IBOutlet UITableView *MineMeFocusTableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) MJRefreshAutoNormalFooter *footer;

@end

@implementation MineMefocusViewController

static NSString *mainCellIdentifier = @"MainTableViewCell";

- (void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    [self.MineMeFocusTableView reloadData];
}

- (MJRefreshAutoNormalFooter *)footer
{
    if (!_footer) {
        _footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downloadMore)];
    }
    return _footer;
}

- (NSInteger)page
{
    if (!_page) {
        _page = 1;
    }
    return _page;
}

- (void)setupView
{
    _MineMeFocusTableView.delegate =self;
    _MineMeFocusTableView.dataSource = self;
    _MineMeFocusTableView.tableFooterView = [[UIView alloc] init];
    
    _MineMeFocusTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self download];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的关注";
    [self setupView];
    [self download];
    // Do any additional setup after loading the view from its nib.
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineMeFocusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mainCellIdentifier];
    if (!cell)
    {
        cell = [[UINib nibWithNibName:@"MineMeFocusTableViewCell" bundle:nil] instantiateWithOwner:self options:nil].lastObject;
    }
    MineFocusListModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    cell.isHidden = NO;
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (void)clickCancelFocusBtnWith:(MineMeFocusTableViewCell *)cell
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"Uid"] = cell.model._id;
    CancelFocusOperator *operator = [[CancelFocusOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        if ([baseModel.code isEqualToString:@"1"]) {
            [self showToastMessage:baseModel.msg];
            [self download];
        }
    } failBlock:^(BaseModel *baseModel) {
        
    }];
}

- (void)download
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"Type"] = nil;
    dic[@"Page"] = [NSString stringWithFormat:@"%ld", self.page];
    dic[@"Row "] = @"30";
    MineFocusOperator *operator = [[MineFocusOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        [self.MineMeFocusTableView.mj_header endRefreshing];
        if ([baseModel.code isEqualToString:@"1"]) {
            self.dataArray = [NSMutableArray arrayWithArray:operator.data.data.list];
            if (self.dataArray.count < 30) {
                self.MineMeFocusTableView.mj_footer = nil;
            }else{
                self.MineMeFocusTableView.mj_footer = self.footer;
                self.page ++;
            }

        }
    } failBlock:^(BaseModel *baseModel) {
        [self.MineMeFocusTableView.mj_header endRefreshing];
    }];
}

- (void)downloadMore
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"Type"] = nil;
    dic[@"Page"] = [NSString stringWithFormat:@"%ld", self.page];
    dic[@"Row "] = @"30";
    MineFocusOperator *operator = [[MineFocusOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        [self.MineMeFocusTableView.mj_footer endRefreshing];
        if ([baseModel.code isEqualToString:@"1"]) {
            NSMutableArray *array = [NSMutableArray arrayWithArray:self.dataArray];
            [array addObjectsFromArray:operator.data.data.list];
            self.dataArray = array;
            if (operator.data.data.list.count < 30) {
                self.MineMeFocusTableView.mj_footer = nil;
            }else{
                self.MineMeFocusTableView.mj_footer = self.footer;
                self.page ++;
            }
        }
    } failBlock:^(BaseModel *baseModel) {
        [self.MineMeFocusTableView.mj_footer endRefreshing];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
